package il.ac.braude.sona.ui.forms;



import il.ac.braude.sona.ui.skeleton.SONAFrame;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JLabel;
import javax.swing.JPanel;




public class FormFrame extends SONAFrame implements ActionListener, WindowListener {
	private FormPanel mFormPanel;
	private JPanel viewPort = new JPanel();
	private ArrayList<FormListener> mFormListeners = new ArrayList<FormListener>();
	private ExecutorService es = Executors.newSingleThreadExecutor();
	private JLabel errorLabel = new JLabel();
	public FormFrame(String title, String okButton) {
		setTitle(title);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		Button ok = new Button(okButton);
		ok.addActionListener(this);
		buttonPanel.add(ok);
		getContentPane().add(buttonPanel,BorderLayout.SOUTH);
		viewPort.setOpaque(false);
		viewPort.setLayout(new BorderLayout());
		errorLabel.setVisible(false);
		errorLabel.setOpaque(false);
		errorLabel.setHorizontalAlignment(JLabel.CENTER);
		errorLabel.setVerticalAlignment(JLabel.CENTER);
		getContentPane().add(viewPort,BorderLayout.CENTER);
		addWindowListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (mFormPanel == null) return;
		final ArrayList<String> list = mFormPanel.getResult();
		es.execute(new Runnable() {

			@Override
			public void run() {
				for (FormListener l : mFormListeners)
					l.onFormSubmited(FormFrame.this,list);
			}
			
		});
	}
	public void setForm(FormPanel formPanel) {
		viewPort.removeAll();
		viewPort.add(mFormPanel=formPanel);
		viewPort.add(errorLabel,BorderLayout.SOUTH);
	}
	public void setDefaultValues(ArrayList<String> arrayList) {
		mFormPanel.setDefaultValues(arrayList);
	}

	public void showMessege(String tetx) {
		errorLabel.setText(tetx);
		errorLabel.setVisible(true);
		pack();
	}
	public void hideMessege() {
		errorLabel.setVisible(false);
		pack();
	}
	public void addFormListener(FormListener listener) {
		if (!mFormListeners.contains(listener)) mFormListeners.add(listener);
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		es.execute(new Runnable() {
			@Override
			public void run() {
				for (FormListener l : mFormListeners)
					l.onFormCanceled(FormFrame.this);
			}
		});
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
